﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using Sql.TestEntities;
using System.Linq;

namespace Sql.Test
{
    [TestClass]
    public class ComprehensiveTest
    {
        [ClassInitialize]
        public static void Initialize(TestContext context)
        {
            var dir = Util.DirectoryHelper.GetDataDirectory();
            string connStr = string.Format("Data Source=(LocalDB)\\v11.0;AttachDbFilename=\"{0}\\TestDB.mdf\";Integrated Security=True", dir);
            Sql.Data.Store.AddConnection("TestDB", connStr);
            Sql.Data.Store.GenerateQueryText = true;
        }

        [TestMethod]
        public void BulkInsertTest()
        {
            List<Person> people = new List<Person>();
            for (var i = 0; i < 10000; i++)
            {
                people.Add(new Person
                {
                    FirstName = string.Format("{0}-BulkInsertTest", i),
                    LastName = "last name",
                    Age = i
                });
            }

            people.CreateAll();

            var results = Data.Store.Query<Person>("SELECT * FROM [Person]");
            Assert.AreEqual(10000, results.Count());
        }

        [TestMethod]
        public void InsertTest()
        {
            var person = new Person
            {
                FirstName = "1-InsertTest",
                LastName = "last name",
                Age = 32
            };

            person.Create();

            var results = Data.Store.Query<Person>("SELECT * FROM [Person]");
            Assert.AreEqual(10001, results.Count());
        }

        [TestMethod]
        public void SelectTest()
        {
            var results = Data.Store.Query<Person>("SELECT * FROM [Person]");
            Assert.AreEqual(10001, results.Count());
        }

        [TestMethod]
        public void SelectAllTest()
        {
            var results = Data.Store.All<Person>();
            var count = results.Count();
        }

        [TestMethod]
        public void SelectAllLinqTest()
        {
            var results = from people in Data.Store.All<Person>() where people.Age % 2 == 0 select people;
            results = Data.Store.All<Person>().Where(x => x.Age % 2 == 0);
            var count = results.Count();
        }

        [TestMethod]
        public void SelectFilterTest()
        {
            var name = "%";
            var age = 100;
            var results = Data.Store.QueryDynamic("EXEC dbo.testsp @FirstName, @Age",
                Parameter.Create("FirstName", name),
                Parameter.Create("Age", null)).ToList();

            var r = Data.Store.CurrentSqlQuery;
        }

        [TestMethod]
        public void SelectMultipleTest()
        {
            var results = Data.Store.QueryMultiple("SELECT * FROM [Person]; SELECT * FROM SimplePerson;", SqlResult.Tables("Person", "SimplePerson"));
        }

        [TestMethod]
        public void SelectOneTest()
        {
            var result = Data.Store.Scalar<Person>("SELECT * FROM [Person]");
            Assert.IsInstanceOfType(result, typeof(Person));
        }

        [TestMethod]
        public void UpdateTest()
        {
            var result = Data.Store.Scalar<Person>("SELECT * FROM [Person]");
            result.IsActive = true;
            result.LastName = "Something Else";
            result.Age = 76;
            result.Update();
        }

        [TestMethod]
        public void DeleteTest()
        {
            var result = Data.Store.Scalar<Person>("SELECT * FROM [Person]");
            result.Delete();
            var results = Data.Store.Query<Person>("SELECT * FROM [Person]");
            Assert.AreEqual(10000, results.Count());
        }

        [TestMethod]
        public void SimplePOCOTest()
        {
            var results = Data.Store.Query<SimplePerson>("SELECT * FROM [Person]").ToList();
            var name = "1%";
            results = Data.Store.Query<SimplePerson>("SELECT * FROM Person WHERE FirstName LIKE @Name",
                Parameter.Create("Name", null), 
                Parameter.Create("Test", name)).ToList();
        }

        [TestMethod]
        public void TruncateTest()
        {
            Data.Store.Query("TRUNCATE TABLE [Person]");
            var results = Data.Store.Query<Person>("SELECT * FROM [Person]");
            Assert.AreEqual(0, results.Count());
        }
    }
}
